import {
  apiOrderList
} from '../other/api'

import { regeneratorRuntime, dayjs } from '../other/bundle'

const initState = {
  auditActivityList: {
    actionStatus: 'INIT',
    data: {}
  }
}

export const getOrderList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiOrderList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.map(i => i.uiUpdateTime = dayjs(i.update_time * 1000).format('YYYY-MM-DD'))
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_ACTIVITY_IMAGE' })
    return data;
  }
}

export const reducer = (state = initState, action) => {
  switch (action.type) {
    // 广告位物料数量
    case 'ASYNC_POSITION_NUM': {
      const { num, index } = action.data
      state.storePosition.data[index].tpl.num = num
      return state
    }
  }
}